package cn.yunhe.java.service;

import cn.yunhe.java.convert.AppCovert;
import cn.yunhe.java.dto.AppDTO;
import cn.yunhe.java.entity.App;
import cn.yunhe.java.entity.Merchant;
import cn.yunhe.java.mapper.AppMapper;
import cn.yunhe.java.mapper.MerchantMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.RandomUuidUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName : AppServiceImpl
 * @Author : qawine
 * @Date: 2021/3/24 19:50
 * @Description : AppService实现类
 */
@Service
public class AppServiceImpl implements AppService {

    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public AppDTO createApp(Long merchantId, AppDTO app) throws BusinessException {
        //校验商户是否通过资质审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        // 校验是否通过审核
        if (!"2".equals(merchant.getAuditStatus())) {
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        // 校验应用名称是否存在
        if(isExistAppName(app.getAppName())){
            throw new BusinessException(CommonErrorCode.E_200004);
        }

        //保存应用信息
        app.setAppId(RandomUuidUtil.getUUID());
        app.setMerchantId(merchant.getId());
        App entity = AppCovert.INSTANCE.dto2entity(app);
        appMapper.insert(entity);
        return AppCovert.INSTANCE.entity2dto(entity);
    }

    /**
     * 校验应用名是否已被使用
     * @param appName
     * @return
     */
    public Boolean isExistAppName(String appName) {
        Integer count = appMapper.selectCount(
                new QueryWrapper<App>().lambda()
                        .eq(App::getAppName, appName)
        );
        return count.intValue() > 0;
    }

    /**
     *  根据商户id 查询该商户下的应用列表
     * @author qawine
     * @param merchantId  商户ID
     * @return java.util.List<com.huiminpay.merchant.api.dto.AppDTO>
     * @date 2021/3/24 20:28
     */
    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        List<App> apps = appMapper.selectList(new QueryWrapper<App>()
                .lambda().eq(App::getMerchantId,merchantId));
        List<AppDTO> appDTOS = AppCovert.INSTANCE.listEntity2dto(apps);
        return appDTOS;

    }

    /**
     * 根据业务 app_id 查询应用
     * @author qawine
     * @param id
     * @return com.huiminpay.merchant.api.dto.AppDTO
     * @date 2021/3/24 20:28
     */
    @Override
    public AppDTO getAppById(String id) throws BusinessException {
        App app = appMapper.selectOne(new QueryWrapper<App>()
                .lambda().eq(App::getAppId, id));
        return AppCovert.INSTANCE.entity2dto(app);
    }

}
